package com.ruoyi.mysystem.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mysystem.domain.SysCollection;
import com.ruoyi.mysystem.domain.SysGoodsCollection;
import com.ruoyi.mysystem.mapper.SysCollectionMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mysystem.mapper.SysGoodsMapper;
import com.ruoyi.mysystem.domain.SysGoods;
import com.ruoyi.mysystem.service.ISysGoodsService;

/**
 * 商品管理Service业务层处理
 * 
 * @author bai
 * @date 2024-01-23
 */
@Service
public class SysGoodsServiceImpl implements ISysGoodsService 
{
    @Autowired
    private SysGoodsMapper sysGoodsMapper;

    @Autowired
    private SysCollectionMapper sysCollectionMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询商品管理
     * 
     * @param goodsId 商品管理主键
     * @return 商品管理
     */
    @Override
    public SysGoods selectSysGoodsByGoodsId(Long goodsId)
    {
        return sysGoodsMapper.selectSysGoodsByGoodsId(goodsId);
    }

    /**
     * 查询商品管理列表
     * 
     * @param sysGoods 商品管理
     * @return 商品管理
     */
    @Override
    public List<SysGoods> selectSysGoodsList(SysGoods sysGoods)
    {
        return sysGoodsMapper.selectSysGoodsList(sysGoods);
    }

    /**
     * 新增商品管理
     * 
     * @param sysGoods 商品管理
     * @return 结果
     */
    @Override
    public int insertSysGoods(SysGoods sysGoods)
    {
        sysGoods.setCreateTime(DateUtils.getNowDate());
        return sysGoodsMapper.insertSysGoods(sysGoods);
    }

    /**
     * 修改商品管理
     * 
     * @param sysGoods 商品管理
     * @return 结果
     */
    @Override
    public int updateSysGoods(SysGoods sysGoods)
    {
        return sysGoodsMapper.updateSysGoods(sysGoods);
    }

    /**
     * 批量删除商品管理
     * 
     * @param goodsIds 需要删除的商品管理主键
     * @return 结果
     */
    @Override
    public int deleteSysGoodsByGoodsIds(Long[] goodsIds)
    {
        return sysGoodsMapper.deleteSysGoodsByGoodsIds(goodsIds);
    }

    /**
     * 删除商品管理信息
     * 
     * @param goodsId 商品管理主键
     * @return 结果
     */
    @Override
    public int deleteSysGoodsByGoodsId(Long goodsId)
    {
        return sysGoodsMapper.deleteSysGoodsByGoodsId(goodsId);
    }

    @Override
    public HashMap goodsDetails(Long userId, Long goodsId) {
        HashMap detailsMap = new HashMap();
        //先查询到商品详情
        SysGoods sysGoods = sysGoodsMapper.selectSysGoodsByGoodsId(goodsId);
        detailsMap.put("goodsDetails",sysGoods);

        //再根据用户id和商品id去收藏表查看是否存在
        SysCollection sysCollection = sysCollectionMapper.selectSysCollectionByUserIdAndGoodsId(userId, goodsId);
        if (sysCollection != null){
            detailsMap.put("goodsIsCollected",1);
        }else {
            detailsMap.put("goodsIsCollected",0);
        }

        //再根据商品中的发布者去查询发布者信息
        String createBy = sysGoods.getCreateBy();
        SysUser sysUser = sysUserMapper.selectUserByUserName(createBy);
        detailsMap.put("goodsSellerDetails",sysUser);
        return detailsMap;
    }

    @Override
    public List<SysGoods> selectSysGoodsCollectionList(SysGoodsCollection sysGoodsCollection) {
        List<SysGoods> sysGoodsCollections = sysGoodsMapper.selectSysGoodsCollectionList(sysGoodsCollection);
        //
        return sysGoodsCollections;
    }
}
